package remote.layouts
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	
	import remote.controls.Component;
	import remote.enums.HAlgin;
	import remote.enums.VAlgin;
	
	
	/**
	 * 竖直布局
	 * @author 银魂飞雪
	 * @createDate 2011-3-6
	 */
	public class VerticalLayout extends PaddingLayout
	{
		private var _gap:Number;
		private var _valgin:String;
		private var _halgin:String;
		
		public function VerticalLayout(target:Component = null, gap:Number = 5, valgin:String = "top", halgin:String = "left", 
								   paddingLeft:Number=0, paddingTop:Number=0, paddingRight:Number=0, paddingBottom:Number=0)
		{
			super(target, paddingLeft, paddingTop, paddingRight, paddingBottom);
			_gap = gap;
			_valgin = valgin;
			_halgin = halgin;
		}
		
		override public function layout():void
		{
			if(elements.length == 0)
				return;
			var childRect:Rectangle = childrenRect;
			var top:Number = paddingTop;
			switch(_valgin)
			{
				case VAlgin.MIDDLE:
					top = Math.max(paddingTop, paddingTop + (target.height - paddingTop - paddingBottom - childRect.height) / 2);
					break;
				case VAlgin.BOTTOM:
					top = Math.max(paddingTop, target.height - paddingBottom - childRect.height);
					break;
			}
			for(var i:int = 0; i < elements.length; i++)
			{
				var child:DisplayObject = elements[i] as DisplayObject;
				child.y = top;
				top += child.height + _gap;
				switch(_halgin)
				{
					case HAlgin.LEFT:child.x = paddingLeft;break;
					case HAlgin.CENTER:child.x = Math.max(paddingLeft, paddingLeft + (target.width - paddingLeft - paddingRight) / 2 - child.width / 2);break;
					case HAlgin.RIGHT:child.x = Math.max(paddingLeft, target.width - paddingRight - child.width);break;
				}
			}
		}
		
		override public function get childrenRect():Rectangle
		{
			if(elements.length == 0)
			{
				return new Rectangle();
			}
			var width:Number = 0, height:Number = _gap * (elements.length - 1);
			for each(var child:DisplayObject in elements)
			{
				height += child.height;
				if(child.width > width)
					width = child.width;
			}
			return new Rectangle(paddingLeft, paddingTop, width, height);
		}
		
		/**
		 * 子项之间的间隔 
		 * 
		 */		
		public function get gap():Number
		{
			return _gap;
		}
		
		public function set gap(value:Number):void
		{
			_gap = value;
		}
		
		/**
		 * 竖直方向对齐方式 
		 * 
		 */		
		public function get valgin():String
		{
			return _valgin;
		}
		
		public function set valgin(value:String):void
		{
			_valgin = value;
		}
		
		/**
		 * 水平方向对齐方式 
		 * 
		 */		
		public function get halgin():String
		{
			return _halgin;
		}
		
		public function set halgin(value:String):void
		{
			_halgin = value;
		}
	}
}